<template>
  <div style="display: flex">
    <div style="flex: 1;margin-right: 10px">
      <h1>待选择商品</h1>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="产品编码" prop="productCode">
          <el-input
            v-model="queryParams.productCode"
            placeholder="请输入产品编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="商品型号" prop="modal">
          <el-input
            v-model="queryParams.modal"
            placeholder="请输入商品型号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" ref="productTable" :data="productList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-row :gutter="10">
                  <el-col :span="12">
                    <el-form-item label="产品编码">
                      {{props.row.productCode}}
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="产品类型">
                      {{props.row.productTypeName}}
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="10">
                  <el-col :span="12">
                    <el-form-item label="计数单位">
                      <span>{{ props.row.unitName }}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="尺寸单位">
                      <dict-tag :options="dict.type.wms_product_cc_unit" :value="props.row.ccUnit"/>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :span="12">
                  <el-col :span="12">
                    <el-form-item label="尺寸">
                      <span v-if="props.row.longWith || props.row.weight || props.row.height">{{ props.row.longWith + '*' + props.row.weight + '*' + props.row.height }}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="重量">
                      <span>{{ props.row.zl }}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-form-item label="详情">
                  <span>{{ props.row.detail }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="产品名称" align="center" prop="productName" :show-overflow-tooltip="true"/>
          <el-table-column label="产品型号" align="center" prop="modal" :show-overflow-tooltip="true"/>
          <el-table-column label="价格（最近一次询价）" align="center" prop="price" />
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        :page-sizes="[5, 10]"
        @pagination="getProductList"
      />
    </div>
    <div style="flex: 1">
      <h1 style="margin-bottom: 83px">已选择商品</h1>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="handleSelectedProductDelete"
          >删除</el-button>
        </el-col>
      </el-row>
      <el-table :data="selectedProductList" @selection-change="handleSelectedProductSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="产品编码">
                    {{props.row.productCode}}
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="产品类型">
                    {{props.row.productTypeName}}
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="计数单位">
                    <span>{{ props.row.unitName }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="尺寸单位">
                    <dict-tag :options="dict.type.wms_product_cc_unit" :value="props.row.ccUnit"/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :span="12">
                <el-col :span="12">
                  <el-form-item label="尺寸">
                    <span v-if="props.row.longWith || props.row.weight || props.row.height">{{ props.row.longWith + '*' + props.row.weight + '*' + props.row.height }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="重量">
                    <span>{{ props.row.zl }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="详情">
                <span>{{ props.row.detail }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="产品名称" align="center" prop="productName" :show-overflow-tooltip="true"/>
        <el-table-column label="产品型号" align="center" prop="modal" :show-overflow-tooltip="true"/>
        <el-table-column label="价格（最近一次询价）" align="center" prop="price" />
      </el-table>
    </div>
  </div>
</template>
<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listProduct} from "@/api/wms/product";

export default {
  name: "SelectedProduct",
  components: {Treeselect},
  dicts: ['wms_product_cc_unit'],
  props: {
    filterKeys: {
      type: Array,
      default() {
        return [];
      }
    },
    openFlag: {
      type: Boolean,
      default: false
    }
  },
  data(){
    return {
      loading:false,
      //商品列表
      productList:[],
      // 选择商品列表
      selectedProductList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        productName: null,
        shortName: null,
        productCode: null,
        filterKeys: []
      },
      // 已选中的商品code集合
      ids: [],
      // 当前页面选中code集合
      curIds: [],
      // 选中的要删除的商品code
      delIds:[],
      // 总数
      total: 0,
      tableLoadFlag: false
    }
  },
  mounted() {
    this.getProductList();
  },
  methods:{
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getProductList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询商品列表 */
    getProductList() {
      let that = this
      that.tableLoadFlag = true
      that.curIds = []
      this.queryParams.filterKeys = this.filterKeys
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.$nextTick(e => {
          that.productList.forEach(item => {
            if (that.ids.indexOf(item.productCode) > -1) {
              this.curIds.push(item.productCode);
              that.$refs.productTable.toggleRowSelection(item,true)
            }
          })
        })
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (this.tableLoadFlag && selection.length === 0) {
        // 页面切换
      } else {
        let ids = selection.map(item => item.productCode)
        if (this.curIds.length > selection.length) {
          // 取消选中节点
          for (let i = 0; i < this.curIds.length; i++) {
            let code = this.curIds[i]
            if (ids.indexOf(code) === -1) {
              this.ids.splice(this.ids.indexOf(code), 1)
              for (let i = 0; i < this.selectedProductList.length; i++) {
                if (this.selectedProductList[i].productCode === code) {
                  this.selectedProductList.splice(i, 1)
                  break
                }
              }
            }
          }
        } else {
          // 选中节点
          selection.forEach(item => {
            if (this.curIds.indexOf(item.productCode) === -1) {
              this.selectedProductList.push(item)
              this.ids.push(item.productCode)
            }
          })
        }
        this.curIds = ids
      }
      this.tableLoadFlag = false
    },
    // 多选框选中数据
    handleSelectedProductSelectionChange(selection) {
      this.delIds = selection.map(item => item.productCode)
    },
    // 删除选择的商品
    handleSelectedProductDelete(){
      this.delIds.forEach(id => {
        for(let i=0;i<this.selectedProductList.length;i++){
          if(this.selectedProductList[i].productCode===id){
            this.$refs.productTable.toggleRowSelection(this.selectedProductList[i],false)
            this.selectedProductList.splice(i,1);
            break
          }
        }
      })
    },
    getSelectedProductList(){
      return this.selectedProductList;
    }
  },
  watch:{
    openFlag: {
      handler(nval, oldval){
        if (nval){
          this.ids = []
          this.delIds = []
          this.selectedProductList = []
          this.resetQuery()
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>
